const base = 'https://movie.douban.com/subject/'
const puppeteer = require("puppeteer")
const mongoose = require("mongoose")
const Movie = mongoose.model("Movie")
// const Category = mongoose.model("Category")


const sleep = time => new Promise(resolve => {
    setTimeout(resolve, time)
})


;(async ()=>{
    console.log('start visit the target page')

    const browser = await puppeteer.launch({
        args: ['--no-sandbox'],
        dumpio: false
    })
    const page = await browser.newPage()

    let movies = await Movie.find({
        $or: [
            {video: {$exists: false}},
            {video: null},
            {cover: {$exists: false}},
            {cover: ''}
        ]
    })

    for (let i = 0, len = movies.length; i<len; i++){
        let movie = movies[i]
        await page.goto(base + movie.doubanId, {
            waitUntil: 'networkidle2'
        })
        await sleep(1000)
        const result = await page.evaluate(()=>{
            var $ = window.$
            var it = $('.related-pic-video')
            if (it && it.length>0) {
                var link = it.attr('href')
                var cover = it.css("backgroundImage")
                var reg = /(http:\/\/|https:\/\/)((\w|=|\.|\/|&|-)+)[^\?]/
                cover = cover.match(reg)[0]
                return {
                    link,
                    cover
                }
            }
            return {}
        })


        let video
        if (result.link) {
            await page.goto(result.link, {
                waitUntil: 'networkidle2'
            })
            await sleep(2000)
            video = await page.evaluate(()=>{
                var $ = window.$
                var it = $('source')

                if (it && it.length>0){
                    return it.attr('src')
                }
                return ''
            })
        }
        movie.video = video || ''
        movie.cover = result.cover || ''
        // console.log(movie)
        await movie.save()
        // const data = {
        //     video,
        //     doubanId,
        //     cover: result.cover
        // }
        
    }
    console.log("end..............")
    browser.close()
    // process.send("end")
    // process.exit(0)
})();